package com.fdb.efp.console.api.rest;

import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.PubConstant;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.e4a.service.vo.SDataPersVO;
import com.irdstudio.efp.e4a.service.vo.SRoleDataRuleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SRoleDataRuleController extends AbstractController  {

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;


	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/role/data/rules", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRoleDataRuleVO>> querySRoleDataRuleAll(@RequestBody SRoleDataRuleVO vo) {
		List<SRoleDataRuleVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
        vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRoleDataRuleService.queryAllRoleOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRoleDataRuleService.queryAllRoleCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRoleDataRuleService.queryAllRoleCurrDownOrg(vo);
			break;
		default:
			outputVo = sRoleDataRuleService.queryAllRoleOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/role/data/rule/{roleCode}{dataPersId}/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRoleDataRuleVO> queryRoleByPk(@PathVariable("roleCode") String roleCode,@PathVariable("dataPersId") String dataPersId) {
		SRoleDataRuleVO inVo = new SRoleDataRuleVO();
				inVo.setRoleCode(roleCode);
				inVo.setDataPersId(dataPersId);
		SRoleDataRuleVO outputVo = sRoleDataRuleService.queryRoleByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param sRoleDataRule
	 * @return
	 */
	@RequestMapping(value="/s/role/data/rule", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteRoleByPk(@RequestBody SRoleDataRuleVO inSRoleDataRuleVo) {
		int outputVo = sRoleDataRuleService.deleteRoleByPk(inSRoleDataRuleVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inSRoleDataRuleVo
	 * @return
	 */
	@RequestMapping(value="/s/role/data/rule", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateRoleByPk(@RequestBody SRoleDataRuleVO inSRoleDataRuleVo) {
		inSRoleDataRuleVo.setPrdCode(inSRoleDataRuleVo.getPrdCode());
		setUserInfoToVO(inSRoleDataRuleVo);
		/**
		 * modify
		 * @since 2019/10/16
		 */
		if(!DataRuleType.CurrOwnerPrd.getValue().equals(inSRoleDataRuleVo.getPermisType())){
			inSRoleDataRuleVo.setPrdCode(null);
			inSRoleDataRuleVo.setPrdCodeStr(null);
		}
		int outputVo = sRoleDataRuleService.updateRoleByPk(inSRoleDataRuleVo);
		if(outputVo == -2){
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setCode(outputVo+"");
			responseData.setRows(outputVo);
			responseData.setMessage("数据权限配置已经存在，不能修改！");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inSRoleDataRuleVo
	 * @return
	 */
	@RequestMapping(value="/s/role/data/rule", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRoleDataRule(@RequestBody SRoleDataRuleVO inSRoleDataRuleVo) {
		setUserInfoToVO(inSRoleDataRuleVo);
		int outputVo = sRoleDataRuleService.insertSRoleDataRule(inSRoleDataRuleVo);
		if(outputVo == -2){
			ResponseData<Integer> responseData = new ResponseData<>();
			responseData.setCode(outputVo+"");
			responseData.setRows(outputVo);
			responseData.setMessage("已存在此数据权限ID配置，不能重复新增");
			responseData.setSuccess(false);
			return responseData;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 数据权限页面，给角色配置访问数据资源权限，pop框
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @returns
	 */
	@RequestMapping(value="/s/data/perss", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDataPersVO>> querySDataPersAll(@RequestBody SDataPersVO vo) {
		List<SDataPersVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
        DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
        setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRoleDataRuleService.queryAllDataOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRoleDataRuleService.queryAllDataCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRoleDataRuleService.queryAllDataCurrDownOrg(vo);
			break;
		default:
			outputVo = sRoleDataRuleService.queryAllDataOwner(vo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/data/pers/{dataPersId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SDataPersVO> queryDataByPk(@PathVariable("dataPersId") String dataPersId) {
		SDataPersVO inVo = new SDataPersVO();
				inVo.setDataPersId(dataPersId);
		SDataPersVO outputVo = sRoleDataRuleService.queryDataByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * @param sDataPers
	 * @return
	 */
	@RequestMapping(value="/s/data/pers", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteDataByPk(@RequestBody SDataPersVO inSDataPersVo) {
		int outputVo = sRoleDataRuleService.deleteDataByPk(inSDataPersVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * @param inSDataPersVo
	 * @return
	 */
	@RequestMapping(value="/s/data/pers", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateDataByPk(@RequestBody SDataPersVO inSDataPersVo) {
		setUserInfoToVO(inSDataPersVo);
		int outputVo = sRoleDataRuleService.updateDataByPk(inSDataPersVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * @param inSDataPersVo
	 * @return
	 */
	@RequestMapping(value="/s/data/pers", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSDataPers(@RequestBody SDataPersVO inSDataPersVo) {
		setUserInfoToVO(inSDataPersVo);
		int outputVo = sRoleDataRuleService.insertSDataPers(inSDataPersVo);
		return getResponseData(outputVo);
	}
}
